package com.advertise.strutsAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.advertise.model.DHTMLXGridLine;
import com.advertise.model.Role;
import com.advertise.model.User;
import com.advertise.service.RoleService_IFace;
import com.advertise.service.UserService_IFace;
import com.advertise.tool.DhtmlxHelper;
import com.opensymphony.xwork2.ActionSupport;

public class UserPageAction {

	private UserService_IFace userService;		//@ property 1
	
	private RoleService_IFace roleService;		//@ property 2
	

	public RoleService_IFace getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService_IFace roleService) {
		this.roleService = roleService;
	}

	public UserService_IFace getUserService() {
		return userService;
	}

	public void setUserService(UserService_IFace userService) {
		this.userService = userService;
	}

	private String xmlData;


	public String getXmlData() {
		return xmlData;
	}

	public void setXmlData(String xmlData) {
		this.xmlData = xmlData;
	}

	public String showUserPage() {

		return ActionSupport.SUCCESS;
	}

	public String getUserXmldata() {
		List<User> allUsers = userService.getAll();
		List<DHTMLXGridLine> gridLines = new ArrayList<DHTMLXGridLine>();
		for (User user : allUsers) {
			DHTMLXGridLine line = new DHTMLXGridLine();
			line.addFieldValue("" + user.getUserId());

			line.addFieldValue(user.getLoginName());

			line.addFieldValue(user.getUserName());

			line.addFieldValue(user.geteMail());

			// line.addFieldValue(user.getState());

			gridLines.add(line);
		}

		xmlData = DhtmlxHelper.convertListToGridData(gridLines);

		return ActionSupport.SUCCESS;
	}
	
	private User editUser;
	
	public User getEditUser() {
		return editUser;
	}

	public void setEditUser(User editUser) {
		this.editUser = editUser;
	}

	public String createUser(){
		createRoleList();
		
		editUser = new User();
		return ActionSupport.SUCCESS;
	}
	
	private int selectedUserId = 0;
	
	
	
	public int getSelectedUserId() {
		return selectedUserId;
	}

	public void setSelectedUserId(int selectedUserId) {
		this.selectedUserId = selectedUserId;
	}

	public String modifyUser(){
		createRoleList();
		
		editUser = userService.getbyID(selectedUserId);
		
		
		
		return ActionSupport.SUCCESS;
	}
	
	public String saveUser(){
		userService.save(editUser);
		return ActionSupport.SUCCESS;
	}
	
	
	private int userRoleId;

	public int getUserRoleId() {
		return userRoleId;
	}

	public void setUserRoleId(int userRoleId) {
		this.userRoleId = userRoleId;
	}
	
	private List<Map<Integer, String>> roleList;

	public List<Map<Integer, String>> getRoleList() {
		
		return roleList;
	}

	public void setRoleList(List<Map<Integer, String>> roleList) {
		this.roleList = roleList;
	}

	
	public void createRoleList(){
		if(roleList == null){
			roleList = new ArrayList<Map<Integer,String>>();			
		}
		List<Role> allRoles = roleService.getAll();
		
		for(Role role:allRoles){
			Map<Integer,String> map = new HashMap<Integer, String>();
			map.put(role.getRoleId(), role.getRoleName());
			
			roleList.add(map);
		}
	}
	
	
	
}
